﻿#include "config_manager.h"

#include <QDir>
#include <QStandardPaths>
#include <QDebug>
#include <QMutex>
#include <QTextCodec>

ConfigManager *ConfigManager::_pInstance = nullptr;

ConfigManager::ConfigManager(QObject *parent)
    : QObject(parent)
{
    m_winInfoConfig = new QSettings(getConfigPath(), QSettings::IniFormat);
    m_winInfoConfig->setIniCodec(QTextCodec::codecForName("UTF-8"));
}

/**
 * @brief ConfigManager::getInstance
 * @return 返回类实例
 * 获取单例指针
 */
ConfigManager *ConfigManager::instance()
{
    QMutex mutex;
    if (_pInstance == nullptr) {
        mutex.lock();
        if (_pInstance == nullptr)
            _pInstance = new ConfigManager();
        mutex.unlock();
    }
    return _pInstance;
}

/**
 * @brief ConfigManager::getWinInfoConfigPath
 * @return 返回配置文件目录
 * 获取配置文件目录
 */
QString ConfigManager::getConfigPath()
{
#ifdef Q_OS_WIN
    QDir winInfoPath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));

    if (!winInfoPath.exists()) {
        winInfoPath.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
    }

    QString Path(winInfoPath.filePath("config.ini"));
#else
    QString Path(QDir::current().filePath("config.conf"));
    //    qInfo() << Q_FUNC_INFO << Path;
#endif
    return Path;
}

/**
 * @brief ConfigManager::getSettings
 * @return 返回私有成员指针
 * 返回QSettings指针，对文件进行操作
 */
QSettings *ConfigManager::settings()
{
    return this->m_winInfoConfig;
}
